home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************
- "floatingWindow.c"
-
- adapted from the pioneering work by:
-
- Don Melton & Mike Ritter [MacTutor, April, 1988]
- ... and ...
- Thomas Fruin [MacTutor, December 1988]
- ... and ...
- Patrick Doane [his "FLOATING WINDOWS", Version 1.3,]
- [downloaded from "America OnLine" ]
-
- using Symantec's "THINK C", v 5.00
- *********************************************************/
-
-
- #include "protos"
-
- #include "globals.h"
- #include "extern.h"
-
- #include "floatingWindow.h"
-
-
- WindowPeek frontToolWindow,
- backToolWindow,
- frontDocWindow;
-
-
-
-
- void TInitWindows (void) {
-
-
- frontToolWindow =
- backToolWindow =
- frontDocWindow = nil;
-
- } /* TInitWindows */
-
-
-
- WindowPtr TMakeNewTool (short windowID, Ptr wStorage, WindowPtr behind) {
-
- WindowPtr theWindow;
-
-
- if (!gMac2) theWindow = GetNewWindow (windowID, wStorage, behind);
- else theWindow = GetNewCWindow (windowID, wStorage, behind);
-
- if (theWindow != nil) {
- ((WindowPeek) theWindow)->windowKind = toolKind;
- if ( ((WindowPeek) theWindow)->visible ) TShowWindow(theWindow);
- }
-
- return (theWindow);
-
- } /* TMakeNewTool */
-
-
-
- void TCloseWindow (WindowPtr theWindow) {
-
-
- // Reset our globals such as frontToolWindow:
- THideWindow(theWindow);
- CloseOurWindow(theWindow);
-
- } /* TCloseWindow */
-
-
-
- void TSelectWindow (WindowPtr whichWindow) {
-
- WindowPeek whichPWindow, theFrontWindow;
- short whichWKind, frontWKind;
-
-
- whichPWindow = (WindowPeek) whichWindow;
- theFrontWindow = (WindowPeek) gFW;
- whichWKind = TGetWKind(whichPWindow);
- frontWKind = TGetWKind(theFrontWindow);
-
- if (whichWKind == toolKind) {
-
- THiliteTools(true);
-
- if (frontWKind == systemKind) {
- // Send DA(s) behind frontDocWindow OR backToolWindow:
- if (frontDocWindow != nil)
- TSendDAsBack(frontDocWindow);
- else TSendDAsBack(backToolWindow);
-
- CurDeactive = (WindowPtr) theFrontWindow;
- HiliteWindow(CurDeactive, false);
- }
-
- else /* Window is a tool window, with NO DA on top */
- ;
-
- if ((frontToolWindow != backToolWindow) /* more than one */ &&
- (whichPWindow == backToolWindow))
- backToolWindow = TPrevVisWindow(whichPWindow);
- if (whichPWindow != frontToolWindow) BringToFront(whichWindow);
- frontToolWindow = whichPWindow;
-
- if (frontDocWindow != nil) {
- CurActivate = (WindowPtr) frontDocWindow;
- HiliteWindow(CurActivate, true);
- }
-
- } /* end: toolKind */
-
- else if (whichWKind == userKind) {
-
- THiliteTools(true);
-
- if (frontWKind == systemKind) {
- /* Window is a document window, or a modeless
- ** dialog window, with a Desk Accessory on top. */
-
- if (frontToolWindow) {
- if (whichPWindow != frontDocWindow)
- TBringForward(whichPWindow, backToolWindow, normalCalc);
- TSendDAsBack(whichPWindow);
- }
- else TSendDAsBack(whichPWindow);
-
- CurDeactive = (WindowPtr) theFrontWindow;
- HiliteWindow(CurDeactive, false);
- } /* systemKind */
-
- else {
- /* Window is a document window, or a modeless
- ** dialog window, with NO DA on top. */
-
- if (frontToolWindow == nil)
- SelectWindow(whichWindow);
- else {
- if (whichPWindow != frontDocWindow)
- TBringForward(whichPWindow, backToolWindow, normalCalc);
- if (frontDocWindow && (whichPWindow != frontDocWindow)) {
- CurDeactive = (WindowPtr) frontDocWindow;
- HiliteWindow(CurDeactive, false);
- }
- }
- } /* NO DA on top */
-
- frontDocWindow = whichPWindow;
- CurActivate = (WindowPtr) whichPWindow;
- HiliteWindow(CurActivate, true);
-
- } /* end: userKind */
-
- else /* systemKind */ {
-
- /* DAs do their own thing !! */
-
- }
-
- } /* TSelectWindow */
-
-
-
- void THideWindow (WindowPtr theWindow) {
-
-
- if (theWindow == (WindowPtr)frontToolWindow) {
- if (frontToolWindow == backToolWindow) /* Just one to begin with !! */
- frontToolWindow = backToolWindow = nil;
- else
- frontToolWindow = TNextVisWindow(toolKind, frontToolWindow);
- ;
- HideWindow(theWindow); /* Generates Activate Event. */
- }
-
- else if (TGetWKind((WindowPeek)theWindow) == toolKind) {
- /* A Tool Window in back: */
-
- if ((WindowPeek)theWindow == backToolWindow)
- backToolWindow = TPrevVisWindow((WindowPeek)theWindow);
- ;
- ShowHide(theWindow, false);
- }
-
- else /* userKind */ {
-
- WindowPeek newFrontDocWindow;
-
-
- newFrontDocWindow = TNextVisWindow(userKind, frontDocWindow);
-
- if (frontToolWindow) {
- // So we don't deactivate a hidden window:
- frontDocWindow = nil;
- ShowHide(theWindow, false);
- // Set up globals, CurActivate & CurDeactive:
- if (newFrontDocWindow) TSelectWindow((WindowPtr)newFrontDocWindow);
- }
- else {
- HideWindow(theWindow);
- frontDocWindow = newFrontDocWindow;
- }
-
- } /* userKind */
-
- } /* THideWindow */
-
-
-
- void TShowWindow (WindowPtr theWindow) {
-
-
- if (TGetWKind((WindowPeek)theWindow) == toolKind) {
- if (frontToolWindow == nil)
- frontToolWindow = backToolWindow = (WindowPeek)theWindow;
- else
- frontToolWindow = (WindowPeek)theWindow;
- ;
- DisplayWindow(theWindow, false);
- TSelectWindow(theWindow);
- }
- else /* userKind */ {
- if (frontToolWindow) {
- DisplayWindow(theWindow, false);
- TSelectWindow(theWindow);
- }
- else {
- frontDocWindow = (WindowPeek)theWindow;
- DisplayWindow(theWindow, true); /* Generates Activate Event. */
- }
- }
-
- } /* TShowWindow */
-
-
-
- void THideFloats (void) {
-
- WindowPeek theWindow;
-
-
- if (frontToolWindow) {
- theWindow = frontToolWindow; /* Start with our top one. */
-
- while (theWindow && (theWindow != backToolWindow->nextWindow)) {
- /* Contine until we go beyond the last floating window. */
-
- /* Hide the window and move on to the next: */
- ShowHide((WindowPtr)theWindow, false);
- theWindow = theWindow->nextWindow;
- }
- } /* We have floating window(s) */
-
- } /* THideFloats */
-
-
-
- void TShowFloats (void) {
-
- WindowPeek theWindow;
-
-
- if (frontToolWindow) {
- theWindow = frontToolWindow;
-
- while (theWindow && (theWindow != backToolWindow->nextWindow)) {
- ShowHide((WindowPtr)theWindow, true);
- theWindow = theWindow->nextWindow;
- }
- }
-
- } /* TShowFloats */
-
-
-
- WindowPtr TFrontWindow (short wantedKind) {
-
- WindowPtr theFrontWindow;
- short frontWKind;
-
-
- theFrontWindow = FrontWindow();
- if (theFrontWindow == nil) return (nil);
-
- frontWKind = TGetWKind((WindowPeek)theFrontWindow);
-
- switch (wantedKind) {
-
- case toolKind:
- return ((WindowPtr)frontToolWindow);
- break;
-
- case userKind:
- return ((WindowPtr)frontDocWindow);
- break;
-
- case systemKind:
- if (frontWKind == systemKind) return (theFrontWindow);
- else return (nil);
- break;
-
- case anyKind:
- return (theFrontWindow);
-
- } /* switch */
-
- } /* TFrontWindow */
-
-
-
- void TDragWindow (WindowPtr theWindow, EventRecord *theEvent) {
-
- GrafPtr savePort, wPort;
- RgnHandle oldClip, desktopRgn, copyWStruct;
- Rect limitRect, windStruct, contentRect, deskRect;
- long endMouse;
- short belowTop, htTB, offBottom, noDrag = -32768, hDrag, vDrag;
- Point thePt;
- extern long Sleep;
-
-
- // If dragging with Balloon showing, Floating Window gets overwritten. So ...
- if (gBalloonsUp && frontToolWindow) {
- HideBalloons(true);
-
- /* Need instantaneous update because of Title Bar's balloon: */
- if (gWNE)
- while ( WaitNextEvent(updateMask, theEvent, Sleep, nil) )
- HandleUpdate((WindowPtr) theEvent->message);
- else
- while ( GetNextEvent(updateMask, theEvent) )
- HandleUpdate((WindowPtr) theEvent->message);
- }
-
- GetPort(&savePort);
- GetWMgrPort(&wPort);
- SetPort(wPort);
-
- oldClip = NewRgn();
- GetClip(oldClip);
- desktopRgn = GetGrayRgn();
- SetClip(desktopRgn);
- ClipAbove((WindowPeek)theWindow);
-
- copyWStruct = NewRgn();
- CopyRgn( ((WindowPeek)theWindow)->strucRgn, copyWStruct );
-
- thePt = theEvent->where;
-
- windStruct = (**copyWStruct).rgnBBox;
- contentRect = (**((WindowPeek)theWindow)->contRgn).rgnBBox;
- belowTop = thePt.v - windStruct.top;
- ;
- htTB = contentRect.top - 1 - windStruct.top; // Height of Title Bar.
- if (htTB <= 0) offBottom = 20; // ... if none.
- else offBottom = contentRect.top - thePt.v;
-
- GlobalToLocal(&thePt);
- deskRect = (**desktopRgn).rgnBBox;
- SetRect(&limitRect,
- deskRect.left + 20,
- deskRect.top + belowTop, /* Do NOT hide top of window. */
- deskRect.right - 20,
- deskRect.bottom - offBottom ); /* Do NOT hide Title Bar. */
-
- endMouse = DragGrayRgn(copyWStruct, thePt, &limitRect, &limitRect,
- noConstraint, (ProcPtr) nil);
-
- SetClip(oldClip);
-
- vDrag = HiWord(endMouse);
- hDrag = LoWord(endMouse);
- if ( (vDrag != noDrag) && (hDrag != noDrag) ) {
-
- SetPort(theWindow);
- thePt = topLeft(theWindow->portRect);
- LocalToGlobal(&thePt);
- MoveWindow(theWindow, thePt.h + hDrag, thePt.v + vDrag, false);
-
- /* If dragging a back Document Window or a back DA
- ** WITHOUT the CMD-key pressed, then bring window forward: */
-
- if ( !(theEvent->modifiers & cmdKey) ) {
-
- short theWKind;
-
- theWKind = TGetWKind((WindowPeek)theWindow);
- if (theWKind == toolKind)
- ;
- else if (theWKind == userKind) {
- if ( (WindowPeek)theWindow != frontDocWindow )
- TSelectWindow(theWindow);
- }
- else /* systemKind */ if (theWindow != TFrontWindow(systemKind))
- SelectWindow(theWindow);
-
- } /* CMD-key NOT pressed */
-
- } /* Dragged a little before releasing Mouse within bounds */
-
- DisposeRgn(copyWStruct);
- DisposeRgn(oldClip);
- SetPort(savePort);
-
- ShowBalloons(gBalloonsUp);
-
- } /* TDragWindow */
-
-
-
- short TGetWKind (WindowPeek theWindow) {
-
- short theWKind;
-
-
- theWKind = theWindow->windowKind;
-
- if (theWKind < 0) theWKind = systemKind;
-
- else if (theWKind == dialogKind) theWKind = userKind;
-
- else if (theWKind == toolKind) theWKind = toolKind;
-
- else if (theWKind >= userKind) theWKind = userKind;
-
- return (theWKind);
-
- } /* TGetWKind */
-
-
-
- void THiliteTools (Boolean hilite) {
-
- WindowPeek loopWindow;
- Boolean done = false;
-
-
- if (frontToolWindow != nil) {
- for ( loopWindow = frontToolWindow;
- !done && (loopWindow != nil);
- loopWindow = loopWindow->nextWindow ) {
-
- if ( (TGetWKind(loopWindow) == toolKind) &&
- loopWindow->visible ) {
- HiliteWindow((WindowPtr)loopWindow, hilite);
- if (loopWindow == backToolWindow) done = true;
- }
-
- } /* for-loop */
- }
-
- } /* THiliteTools */
-
-
-
- void TSuspend (void) {
-
-
- if (frontDocWindow) {
- HiliteWindow((WindowPtr)frontDocWindow, false);
- DoDeactivate((WindowPtr)frontDocWindow);
- }
-
- THideFloats();
-
- } /* TSuspend */
-
-
-
- void TResume (void) {
-
-
- TShowFloats();
-
- if (frontDocWindow) TSelectWindow((WindowPtr) frontDocWindow);
- else THiliteTools(true);
-
- } /* TResume */
-
-
-
- WindowPeek TNextVisWindow (short wantedKind, WindowPeek thePWindow) {
-
- WindowPeek loopWindow, wantedWindow;
-
-
- for (wantedWindow = nil, loopWindow = thePWindow->nextWindow;
- (wantedWindow == nil) && (loopWindow != nil);
- loopWindow = loopWindow->nextWindow) {
-
- if (loopWindow->visible)
- if ( (wantedKind == anyKind) ||
- (wantedKind == TGetWKind(loopWindow)) )
- wantedWindow = loopWindow;
-
- }
-
- return (wantedWindow);
-
- } /* TNextVisWindow */
-
-
-
- WindowPeek TPrevVisWindow (WindowPeek thePWindow) {
-
- WindowPeek loopWindow, prevWindow, theFrontWindow;
- Boolean done = false;
-
-
- theFrontWindow = (WindowPeek)FrontWindow();
-
- if ( !theFrontWindow->visible || (thePWindow == theFrontWindow) ) return (nil);
-
- prevWindow = theFrontWindow;
- loopWindow = prevWindow->nextWindow;
-
- while (!done && (loopWindow != nil)) {
- if (loopWindow == thePWindow) done = true;
- else {
- if (loopWindow->visible) prevWindow = loopWindow;
- loopWindow = loopWindow->nextWindow;
- }
- }
-
- return (prevWindow);
-
- } /* TPrevVisWindow */
-
-
-
- void TBringForward (WindowPeek thePWindow, WindowPeek behindWindow,
- Boolean waitWithCalc) {
-
- RgnHandle coveredRgn;
- GrafPtr savePort;
-
-
- GetPort(&savePort);
- SetPort((WindowPtr)thePWindow);
- ;
- coveredRgn = NewRgn();
-
- // visible part of portRect:
- CopyRgn(((WindowPtr)thePWindow)->visRgn, coveredRgn);
- OffsetRgn(coveredRgn, /* LocalToGlobal */
- (**(thePWindow->contRgn)).rgnBBox.left,
- (**(thePWindow->contRgn)).rgnBBox.top);
- ;
- // result = what's covered by window(s) in front:
- DiffRgn(thePWindow->strucRgn, coveredRgn, coveredRgn);
-
- SendBehind((WindowPtr)thePWindow, (WindowPtr)behindWindow);
-
- /* _PaintOne sets thePort to the Window Manager Port,
- ** so we must set thePort back to theWindow before
- ** we call _CalcVisBehind. */
-
- PaintOne(thePWindow, coveredRgn);
-
- if (!waitWithCalc) {
- SetPort((WindowPtr)thePWindow);
- CalcVisBehind(thePWindow, coveredRgn);
- }
-
- DisposeRgn(coveredRgn);
- ;
- SetPort(savePort);
-
- } /* TBringForward */
-
-
-
- void TSendDAsBack (WindowPeek behindWindow) {
-
- WindowPeek loopWindow = WindowList, daWindow, origBehind;
- RgnHandle overlap;
-
-
- origBehind = behindWindow;
-
- overlap = NewRgn();
-
- while ( (loopWindow != nil) && (loopWindow != origBehind) ) {
-
- if ( (TGetWKind(loopWindow) == systemKind) && loopWindow->visible ) {
- daWindow = loopWindow;
- TBringForward(daWindow, behindWindow, postponeCalc);
- HiliteWindow((WindowPtr) daWindow, false);
- UnionRgn(overlap, daWindow->strucRgn, overlap);
-
- // ... so we maintain the same ordering of DAs we started with:
- behindWindow = daWindow;
-
- /* loopWindow, our DA, has now been moved behind,
- ** so we must continue with the window that was
- ** FORMERLY behind our USED-TO-BE front DA. This
- ** latter window is now on top: */
-
- loopWindow = WindowList;
- continue /* with the while-loop IMMEDIATELY */ ;
- }
-
- loopWindow = loopWindow->nextWindow;
-
- } /* end: while */
-
- CalcVisBehind(origBehind, overlap);
- DisposeRgn(overlap);
-
- } /* TSendDAsBack */
-
-
-
-
- /* { end file "floatingWindow.c" } */
-